<template>
<div class="home-box">
    <el-form :inline="true" :model="searchForm">
      <el-form-item label="员工工号">
        <el-input v-model="searchForm.empId" placeholder="员工工号" />
      </el-form-item>
      <el-form-item label="员工姓名">
        <el-input
          v-model="searchForm.empName"
          placeholder="员工姓名"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="searchEvent" type="primary">查询 </el-button>
      </el-form-item>
      <el-form-item>
        <el-button @click="update" type="primary">重置 </el-button>
      </el-form-item>
      <el-form-item>
        <el-button color="#ff983f" :dark="isDark" plain icon="Download" @click="handleOut"
    >导出</el-button>
      </el-form-item>
      <el-form-item >
        <el-tooltip class="item" effect="dark" content="刷新" placement="top">
          <el-button circle icon="Refresh" @click="update" />
        </el-tooltip>
      </el-form-item>
    </el-form>
  </div>
    <el-table
      :data="ScoreRuleList"
      border
      style="width: 90%; margin: 20px auto;border-radius: 8px; margin: 20px auto;box-shadow: 5px 5px 10px rgba(64, 158, 255, 0.5);"
      :header-cell-style="{
          'background': '#337ecc !important',
          'color': '#ffffff',
          'border': 'none !important'
        }">
      <el-table-column prop="empName" label="被考核人" />
	  
	  <el-table-column prop="position" label="岗位名称" width="150" />
	  <el-table-column prop="depName" label="部门名称" width="150" />
      <el-table-column prop="name" label="条目" width="200" />  
      <el-table-column prop="targetNum" label="单价/￥"  width="200" />
      <el-table-column prop="workOrder" label="数量" width="200" />
      <el-table-column prop="quality" label="质量/%" width="200" />
	  <el-table-column prop="difficulty" label="难度系数/%" width="200" />
      <el-table-column
      prop="state"
      label="当前状态"
      width="95"
    >
      <template #default="scope">
        <el-tag
          :type="scope.row.tag === 'Home' ? '' : 'success'"
          disable-transitions
          >{{ scope.row.state === 0 ? "未计入" : "审核通过" }}</el-tag
        >
      </template>
    </el-table-column>
      <el-table-column label="操作">
      <template #default="scope">
		  <!-- 危险 先隐藏 -->
        <!-- <el-button
          size="small"
          type="danger"
          @click="handleDelete(scope.$index, scope.row)"
          >删除</el-button
        > -->
      </template>
    </el-table-column>
    </el-table>
    <pagination
    class="pagination"
    v-show="ScoreListPage.total > 0"
    :total="ScoreListPage.total"
    v-model:page="searchForm.page"
    v-model:limit="ScoreListPage.size"
    @pagination="handlePagination"
  />
</template>

<script setup>
import { ref, onBeforeMount, reactive, getCurrentInstance,watch } from "vue";
import {getEmpPieceNowPage,reDelete,getEmpPieceNowSearch} from "@/api/empPiece"
import { ElMessage } from "element-plus";
import {downLoadEmpPiece} from "@/api/downExcel"

const searchForm = ref({
  empId: "",
  empName: "",
  page: 1,
  page_size: 10,
});
const ScoreListPage = ref([]);
const ScoreRuleList = ref([]);
onBeforeMount(() => {
  getEmpPieceNowPage(searchForm.value).then((data) => {
    ScoreListPage.value = data.data;
    ScoreRuleList.value = data.data.records;
  });
});
watch(
  ScoreRuleList,
  (newVal) => {
    
  },
  { deep: true }
);

const handleOut=()=>{
  downLoadEmpPiece()
}

const searchEvent=()=>{
  getEmpPieceNowSearch(searchForm.value).then((data) => {
    ScoreListPage.value = data.data;
    ScoreRuleList.value = data.data.records;
  });
}

const handlePagination=(value)=>{
  if(searchForm.value.empId==""&&searchForm.value.empName==""){
    getEmpPieceNowPage(value).then(data=>{
        ScoreListPage.value = data.data;
        ScoreRuleList.value = data.data.records;
    })
  }
  else{
    searchForm.value.page=value.page;
    searchForm.value.page_size=value.page_size
    getEmpPieceNowSearch(searchForm.value).then(data=>{
      ScoreListPage.value = data.data;
      ScoreRuleList.value = data.data.records;
    })
  } 
}

let select=reactive()
const DialogVisible = ref(false);
const handleEdit=(index,row)=>{
  DialogVisible.value = true;
  select=row
}
const handleDelete=(index,row)=>{
  reDelete({id:row.empPieceId}).then(data=>{
    if (data.code == 200) {
      ElMessage({
        message: "修正成功",
        type: "success",
      });
      update()
    } else {
      ElMessage.error("修正失败");
    }
  })
}

const update = () => {
  searchForm.value.empId=""
  searchForm.value.empName=""
  getEmpPieceNowPage(searchForm.value).then((data) => {
    if (data.code == 200) {
      ElMessage({
        message: "更新成功",
        type: "success",
      });
      ScoreListPage.value = data.data;
      ScoreRuleList.value = data.data.records;
    } else {
      ElMessage.error("更新失败");
    }
  });
};

const close=()=>{
  DialogVisible.value = false;
}
</script>
<style lang="scss" scoped>
.el-card {
  border-radius: 10px;
}
</style>
